//
//  OKSickSearchController.m
//  fast
//
//  Created by answer on 16/5/22.
//  Copyright © 2016年 answer. All rights reserved.
//

#import "OKSickSearchController.h"
#import "OKSickModel.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <MJRefresh/MJRefresh.h>

static NSString *OKSickSearchControllerReusableIdentifier = @"OKSickSearchControllerReusableIdentifier";

@interface OKSickSearchController () <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property (strong, nonatomic) NSArray *data;
@property (strong, nonatomic) NSMutableArray *dataNew;
@property (strong, nonatomic) UISearchBar *search;

@end

@implementation OKSickSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:OKSickSearchControllerReusableIdentifier];
    
    // 自定义导航栏左侧按钮
    //self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_button_back_01"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.titleView = self.search;
    self.search.delegate = self;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"   搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchView)];

    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 设置普通状态的动画图片
    UIImage *idleImages = [UIImage imageNamed:@"v2_pullRefresh1"];
    UIImage *pullingImages = [UIImage imageNamed:@"v2_pullRefresh2"];
    NSArray *array = @[idleImages, pullingImages];
    
    [header setImages:array forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:array forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [header setImages:array forState:MJRefreshStateRefreshing];
    // 设置header
    self.tableView.mj_header = header;
}

- (void)loadNewData {
    [NSThread sleepForTimeInterval:1.0];
    [self.tableView.mj_header endRefreshing];
}
#pragma mark - 右侧导航栏点击事件
- (void)searchView {
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSString *str = self.search.text;
    if (str.length == 0) {
        for (id model in self.data) {
            [self.dataNew addObject:model];
        }
        [self.tableView reloadData];
        return;
    }
    
    [self.dataNew removeAllObjects];
    //NSLog(@"%zd",self.data.count);
    
    
    
    for (NSInteger i = 0; i < self.data.count; i ++) {
        OKSickModel *model = self.data[i];
        //NSLog(@"%@", model.ci3_name);
        if ([model.ci3_name containsString:str]) {
            
            [self.dataNew addObject:model];
        }
    }
    [self.tableView reloadData];
}

#pragma mark - 请求数据
- (void)loadData {
    
    [OKSickModel requestSickSegmentWithParams:self.params completion:^(NSArray *result) {
        
        self.data = result;
        
        for (id model in self.data) {
            [self.dataNew addObject:model];
        }
        
        [SVProgressHUD dismiss];
        [self.tableView reloadData];
    }];
}

#pragma mark - 数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataNew.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OKSickSearchControllerReusableIdentifier forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    OKSickModel *model = self.dataNew[indexPath.row];
    //cell.textLabel.text = model.ci3_name;
    
    NSString *str = self.search.text;
    NSRange range = [model.ci3_name rangeOfString:str];
    
    // 给高亮字添加属性
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:model.ci3_name];
//    [AttributedStr addAttribute:NSForegroundColorAttributeName
//                          value:[UIColor redColor]
//                          range:range];
    [AttributedStr addAttribute:NSBackgroundColorAttributeName
                          value:[UIColor redColor]
                          range:range];
    
    cell.textLabel.attributedText = AttributedStr;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OKSickModel *model = self.dataNew[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(sickSegmentCellClick:)]) {
        [self.delegate sickSegmentCellClick:model];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (UISearchBar *)search {
    if (_search == nil) {
        _search = [[UISearchBar alloc] init];
        _search.placeholder = @"疾病细分搜索";
    }
    return _search;
}

- (NSArray *)data {
    if (_data == nil) {
        _data = [NSArray array];
    }
    return _data;
}

- (NSMutableArray *)dataNew {
    if (_dataNew == nil) {
        _dataNew = [NSMutableArray array];
    }
    return _dataNew;
}



@end





